//
//  SettingViewController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/9/9.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class SettingViewController: MainViewController {
    var tableView:SettingTable!
    var cell:SettingCell!
    
    //cell的数据
    var leftString = ["个人资料","通知与消息","清除缓存","投诉与建议","关于山人行","退出登录"]
    var messageNotify = "未开启"
    var cache = "21.5M"
    var centerString = "(开启后可收到三人行的推送)"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "设置"
        UIApplication.sharedApplication().registerForRemoteNotifications()
        self.navigationController?.navigationBar.hidden = false
        self.addTableViewToSettingViewController()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
}

//MARK:--UI设置
extension SettingViewController{
    //添加view进来
    func addTableViewToSettingViewController(){
        tableView = SettingTable(frame: MainBounds, style: .Grouped)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
}

//MARK:--tableView  delegate  datasource
extension SettingViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 15
    }
    
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 1
        case 2:
            return 1
        case 3:
            return 2
        case 4:
            return 1
        default:
            return 0
        }
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        cell = SettingCell(style: .Default, reuseIdentifier: "settingCell")
        switch (indexPath as NSIndexPath).section {
        case 0:
            cell.leftLabel.text = leftString[(indexPath as NSIndexPath).row]
        case 1:
            cell.leftLabel.text = leftString[(indexPath as NSIndexPath).row+1]
            cell.centerLabel.text = centerString
            cell.rightLabel.text = messageNotify
        case 2:
            cell.leftLabel.text = leftString[(indexPath as NSIndexPath).row+2]
            cell.rightLabel.text = cache
        case 3:
            cell.leftLabel.text = leftString[(indexPath as NSIndexPath).row+3]
        case 4:
            let label = cell.leftLabel
            label?.text = leftString[(indexPath as NSIndexPath).row+5]
            label?.textAlignment = .Center
            label?.textColor = Theme.BlueColor
            label?.font = BaseFunc.getCustomFont(14)
            cell.accessoryType = .None
        default:
            break
        }
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        switch (indexPath as NSIndexPath).section {
        case 0:
            self.navigationController?.pushViewController(PersonViewController(), animated: true)
        case 1:
            if UIApplication.sharedApplication().currentUserNotificationSettings()?.types == UIUserNotificationType.None{
                print("没有开启开关")
            }else{
                print("开启了")
            }
        case 3:
            if (indexPath as NSIndexPath).row == 0{
                self.navigationController?.pushViewController(AdviseViewController(), animated: true)
            }else{
                self.navigationController?.pushViewController(AboutSRXController(), animated: true)
            }
        case 4:
            NSUserDefaults.standardUserDefaults().setBool(false, forKey: ISLOGIN)
            OperateUserDefaults.deleteUserDefault()
            self.navigationController?.pushViewController(LogInViewControll(), animated: true)
        default:
            break
        }
    }
}















